package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbMaterialLibraryAndFactory;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbMaterialLibraryAndFactoryDao {
    @Results(id = "pcbMaterialLibraryAndFactoryResultMap", value = {
            @Result(column = "pcb_factory_and_library_id", property = "pcbFactoryAndLibraryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "factory_id", property = "factoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "row_key", property = "rowKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version", property = "version", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })

    /*
     * 查询单个
     */
    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.PCB_MATERIAL_LIBRARY_AND_FACTORY +
            "<where> " +
            "<if test='id != null'>" +
            " and pcb_factory_and_library_id = #{id}" +
            "</if>" +
            "<if test='status != null'>" +
            " and status = #{status}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    PcbMaterialLibraryAndFactory getById(@Param("id") Integer id, @Param("status") Integer status);

    /*
     * 查询factoryId和板材id
     */
    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.PCB_MATERIAL_LIBRARY_AND_FACTORY +
            "<where> " +
            "<if test='rowKey != null'>" +
            " and row_key = #{rowKey}" +
            "</if>" +
            "<if test='factoryId != null'>" +
            " and factory_id = #{factoryId}" +
            "</if>" +
            "<if test='status != null'>" +
            " and status = #{status}" +
            "</if>" +
            "</where> " +
            " order by sort" +
            "</script>")
    @ResultMap("pcbMaterialLibraryAndFactoryResultMap")
    List<PcbMaterialLibraryAndFactory> getByMaterialIdAndFacoryId(@Param("rowKey") String rowKey,
                                                                  @Param("factoryId") Integer factoryId,
                                                                  @Param("status") Integer status);

    @Insert("<script> " +
            "insert into " + DB2TableName.PCB_MATERIAL_LIBRARY_AND_FACTORY + " (factory_id, row_key, version, status, remark, create_time, create_user, update_time, update_user, sort) values" +
            "(#{factoryId}, #{rowKey}, #{version}, #{status}, #{remark}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{sort}) " +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "pcbFactoryAndLibraryId")
    Integer insert(PcbMaterialLibraryAndFactory pcbMaterialLibraryAndFactory);

    @Update("<script>" +
            "update " + DB2TableName.PCB_MATERIAL_LIBRARY_AND_FACTORY +
            "<set>" +
            "<if test='factoryId != null'>" +
            "factory_id = #{factoryId}," +
            "</if>" +
            "<if test='rowKey != null'>" +
            "row_key = #{rowKey}," +
            "</if>" +
            "<if test='version != null'>" +
            "version = #{version}," +
            "</if>" +
            "<if test='remark != null and remark != \"\" '>" +
            "   remark  = #{remark}," +
            "</if>" +
            "<if test='sort != null'>" +
            "    sort = #{sort}," +
            "</if>" +
            "<if test='status != null'>" +
            "    status = #{status}," +
            "</if>" +
            "<if test='createTime != null'>" +
            "    create_time = #{createTime}," +
            "</if>" +
            "<if test='updateTime != null'>" +
            "    update_time = #{updateTime}," +
            "</if>" +
            "<if test='createUser != null and createUser != \"\"'>" +
            "    create_user = #{createUser}," +
            "</if>" +
            "<if test='updateUser != null and updateUser != \"\"'>" +
            "    update_user = #{updateUser}," +
            "</if>" +
            "</set>" +
            "where pcb_factory_and_library_id = #{pcbFactoryAndLibraryId}" +
            "</script>")
    Integer update(PcbMaterialLibraryAndFactory pcbMaterialLibraryAndFactory);

    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_MATERIAL_LIBRARY_AND_FACTORY + " where pcb_factory_and_library_id = #{id} ")
    Integer deleteById(@Param("id") Integer id);
}
